import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-05-07
 * Time: 0:01
 */
public class Main {
    public static void bubbleSort(int[]array) {
        //i表示趟数
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j] > array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flg = true;
                }
                if (flg == false) {
                }
            }
        }
    }
    public static void main(String[] args) {
        int[] array = {1,5,2,3,2,8,4};
        System.out.println("排序之前的数组" + Arrays.toString(array));

        bubbleSort(array);
        System.out.println("排序之后的数组" + Arrays.toString(array));
    }









    public static void main4(String[] args) {
        int[] array = {1,5,2,3,2,8,4};
        Arrays.sort(array);
        System.out.println("排序后的数组" + Arrays.toString(array));
//        int index = Arrays.binarySearch(array, 1);
        int ret = Arrays.binarySearch(array, 2, 4, 3);  //范围是[2,4)
        System.out.println(ret);
    }








    public static void main3(String[] args) {

        int[] array = {1,5,2,3,2,8,4};
        System.out.println("排序前的数组" + Arrays.toString(array));
        Arrays.sort(array);//默认从小到大
        System.out.println("排序后的数组" + Arrays.toString(array));

    }





    public static int binarySerach(int[] array, int key) {
        int left = 0;
        int right = array.length-1;
        int mid = (left + right) / 2;
        while (left <= right){

            if (array[mid] == key) {
                return mid;
            }else if (array[mid] > key) {
                right = mid - 1;
            }else {
                left = mid + 1;
            }
        }
        return -1;
    }

    public static void main2(String[] args) {
        //二分查找
        int[] array = new int[]{1,2,3,4,5};

        int ret = binarySerach(array, 3);
        System.out.println(ret);
    }





    public static int findNumber(int[] array, int key) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] == key) {
                return i;
            }
        }
        return -1;
    }
    public static void main1(String[] args) {
        int[] array = new int[]{1,2,3,4,5};
        System.out.println(findNumber(array, 3));
    }
}
